package cn.lanqiao.facelogin.BaiDuFace;

import cn.lanqiao.facelogin.domain.JsonResult;
import cn.lanqiao.facelogin.domain.LoginData;
import cn.lanqiao.facelogin.domain.ResParameter;
import cn.lanqiao.facelogin.domain.Result;
import cn.lanqiao.facelogin.mapper.LoginDataMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class JSONDataReturnValue {

    @Autowired
    private static LoginDataMapper loginDataMapper;


    public static JsonResult m1(String json) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        String userId = null;
        JSONObject jsonObject = JSONObject.parseObject((String) json);
        try {
            JSONObject result = jsonObject.getJSONObject("result");
            if (result != null) {
                JSONArray userList = result.getJSONArray("user_list");
                for (int i = 0; i < userList.size(); i++) {
                    JSONObject userListJSONObject = userList.getJSONObject(i);
                    hashMap.put("score", userListJSONObject.get("score"));
                    hashMap.put("group_id", userListJSONObject.get("group_id"));
                    hashMap.put("user_id", userListJSONObject.get("user_id"));
                    hashMap.put("user_info", userListJSONObject.get("user_info"));
                    mapList.add(hashMap);
                }
                for (int i = 0; i < mapList.size(); i++) {
                    System.out.println(mapList);
                    Map<String, Object> map = mapList.get(i);
                    Object user_id = map.get("user_id");
                    Object o = map.get("score".toString());
                    userId = String.valueOf(user_id);
                    double d = Double.valueOf(String.valueOf(o)).doubleValue();
                    double a = 90;
                    if (d > a) {
                        return new JsonResult(true, userId);
                    }
                }
            } else {
                return new JsonResult(false, null);
            }
        } catch (Exception e) {
            System.out.println("人脸查询失败");
        }
        return new JsonResult(false, null);
//        }
//        String s = "{\"error_code\":0,\"error_msg\":\"SUCCESS\",\"log_id\":2961560591,\"timestamp\":1645580961,\"cached\":0,\"result\":{\"face_token\":\"479c163847a888d90edf0303490bc867\",\"user_list\":[{\"group_id\":\"18512803590\",\"user_id\":\"yhz\",\"user_info\":\"cba\",\"score\":98.822410583496}]}}\n";
//        System.out.println(s);
//        System.out.println("==========");

    }

    public static Result m2(String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        JSONObject result = jsonObject.getJSONObject("result");
        if (result == null) {
            return new Result(false,ResParameter.DENY_THIS_REQUEST,"","");
        }
        String faceToken = result.getString("face_token");
//        loginData.setFaceToken(faceToken);
//        UpdateWrapper<LoginData> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.eq("phone", phone);
//        updateWrapper.set("faceId", faceToken);
//        int update = loginDataMapper.update(null, updateWrapper);
//        System.out.println(update);
//        boolean b = loginDataMapper.addFaceId(faceToken, phone);
        return new Result(true,null,"",faceToken);
    }


}
